package com.example.work2;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class ItemActivity extends AppCompatActivity {
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;

    String str;
    private TextView textView;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("生命周期状态：","onCreate()");
        setContentView(R.layout.activity_item);

        textView=findViewById(R.id.itemtextView2);
        button1=findViewById(R.id.button);
        button2=findViewById(R.id.button8);
        button3=findViewById(R.id.button9);
        button4=findViewById(R.id.button10);
        imageView=findViewById(R.id.imageView5);

        //查看时间
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ItemActivity.this,ResultActivity.class);
                test1.launch(intent);
            }
        });

        //新建文件
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file1=new File(getFilesDir()+"first.txt");
                try {
                    FileOutputStream stream=new FileOutputStream(file1);

                    stream.write(str.getBytes());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        //课程须知
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputStreamReader inputStreamReader=new
                            InputStreamReader(getResources().getAssets().open("first.txt"));
                    BufferedReader bufferedReader=new BufferedReader(inputStreamReader);
                    String line=",";
                    String result="";
                    while ((line=bufferedReader.readLine())!=null)
                        result+=line;
                    textView.setText(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });


        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2=new Intent(Intent.ACTION_GET_CONTENT);
                intent2.setType("*/*"); //筛选器
                intent2.addCategory(Intent.CATEGORY_OPENABLE);
                test2.launch(Intent.createChooser(intent2,"选择一个文件"));
            }
        });
    }

    public ActivityResultLauncher test1 = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>(){

                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == 9000){
                        Log.i("生命周期状态：","onActivityResultLauncher（）");
                        textView.setText(result.getData().getStringExtra("data"));
                    }
                }
            });

    public ActivityResultLauncher test2 = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>(){

                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == Activity.RESULT_OK){
                        //保存读取到的资源
                        StringBuilder stringBuilder=new StringBuilder();
                        //获取URI
                        Uri uri= Uri.parse(result.getData().toString());
                        //获取文件流
                        //文件中的内容
                        String pathname=uri.getPath();
                        Log.i("hmw：","Intent"+result.getData().toString());
                        Log.i("hmw：","pathname"+pathname);
                        textView.setText(pathname);
                        Bitmap bitmap= BitmapFactory.decodeFile(pathname);
                        imageView.setImageBitmap(bitmap);
                    }
                }
            });


    @Override
    public void onStart() {
        super.onStart();
        Log.i("生命周期状态：","onStart()");
    }
    @Override
    public void onResume() {
        super.onResume();
        Log.i("生命周期状态：","onResume()");
    }
    @Override
    public void onPause() {
        super.onPause();
        Log.i("生命周期状态：","onPause()");
    }
    @Override
    public void onStop() {
        super.onStop();
        Log.i("生命周期状态：","onStop()");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("生命周期状态：","onDestroy()");
    }
}